<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    // async function async1() {
    //   console.log('async1 start');
    //   await async2();
    //   console.log('asnyc1 end');
    // }
    // async function async2() {
    //   console.log('async2');
    // }
    // console.log('script start');
    // setTimeout(() => {
    //   console.log('setTimeOut');
    // }, 0);
    // async1();
    // new Promise(function (reslove) {
    //   console.log('promise1');
    //   reslove();
    // }).then(function () {
    //   console.log('promise2');
    // })
    // console.log('script end');


    // demo01  出自于上面我引用文章的一个例子，我们来根据上面的结论，一步一步分析具体的执行过程。
    // 为了方便理解，我以打印出来的字符作为当前的任务名称
    // setTimeout(function () {
    //   console.log('timeout1');
    // })

    // new Promise(function (resolve) {
    //   console.log('promise1');
    //   for (var i = 0; i < 1000; i++) {
    //     i == 99 && resolve();
    //   }
    //   console.log('promise2');
    // }).then(function () {
    //   console.log('then1');
    // })

    // console.log('global1');


    // demo02
    console.log('golb1');

    setTimeout(function () {
      console.log('timeout1');
      process.nextTick(function () {
        console.log('timeout1_nextTick'); 
      })
      new Promise(function (resolve) {
        console.log('timeout1_promise');
        resolve();
      }).then(function () {
        console.log('timeout1_then')
      })
    })

    setImmediate(function () {
      console.log('immediate1');
      process.nextTick(function () {
        console.log('immediate1_nextTick');
      })
      new Promise(function (resolve) {
        console.log('immediate1_promise');
        resolve();
      }).then(function () {
        console.log('immediate1_then')
      })
    })

    process.nextTick(function () {
      console.log('glob1_nextTick');
    })

    new Promise(function (resolve) {
      console.log('glob1_promise');
      resolve();
    }).then(function () {
      console.log('glob1_then')
    })

    setTimeout(function () {
      console.log('timeout2');
      process.nextTick(function () {
        console.log('timeout2_nextTick');
      })
      new Promise(function (resolve) {
        console.log('timeout2_promise');
        resolve();
      }).then(function () {
        console.log('timeout2_then')
      })
    })

    process.nextTick(function () {
      console.log('glob2_nextTick');
    })
    
    new Promise(function (resolve) {
      console.log('glob2_promise');
      resolve();
    }).then(function () {
      console.log('glob2_then')
    })

    setImmediate(function () {
      console.log('immediate2');
      process.nextTick(function () {
        console.log('immediate2_nextTick');
      })
      new Promise(function (resolve) {
        console.log('immediate2_promise');
        resolve();
      }).then(function () {
        console.log('immediate2_then')
      })
    })
  </script>
</body>

</html>